﻿using System;
using System.Drawing;
using System.Windows.Forms;
using socket_server_01;
using System.Drawing.Printing;
using ThoughtWorks.QRCode.Codec;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private static string print_str = "请输入打印内容.....";
        public static int fontsize = 10;

        public Form1()
        {
            InitializeComponent();
        }



        /// <summary>
        /// 加载完成后开启socket监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            Class1 ps = new Class1();
            ps.run(27777);
        }

        public static void print_last(string str,short pages = 1)
        {
            Form1.print_str = str;
            Form1 obj = new Form1();
            obj.printDocument1.PrinterSettings.Copies = pages;
            obj.printDocument1.Print();
            //MessageBox.Show(ps);
        }


        /// <summary>
        /// 打印测试按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
                //MessageBox.Show(Form1.print_str);
            }
        }

       /// <summary>
       /// 打印预览按钮
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog();
        }

        /// <summary>
        /// 打印设置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

            string str = Form1.print_str;

            Font font = new Font("黑体", Form1.fontsize);
            Brush bru = Brushes.Black;
            StringFormat fmt = new StringFormat();
            
            fmt.LineAlignment = StringAlignment.Near;//左对齐
            PaperSize ps = new PaperSize("Custom Size 1", 430, 125);
            printDocument1.DefaultPageSettings.PaperSize = ps;
            printDocument1.OriginAtMargins = true;
            fmt.FormatFlags = StringFormatFlags.LineLimit;//自动换行                                           
            
            Rectangle r = new Rectangle(100,0, 290,400);//设定文本打印区域 b是左上角坐标，Size是打印区域（矩形） float mmtopt = 2.835f; 单位换算，毫米*2.835=Point单位
            QRCodeEncoder qr = new QRCodeEncoder();
            qr.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qr.QRCodeScale = 2;
            qr.QRCodeVersion = 1;
            qr.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            Bitmap bmp = null;
            bmp = qr.Encode("123456");
            e.Graphics.DrawImage(bmp, 40,0);
            e.Graphics.DrawString(str, font, bru, r, fmt);
        }



        private void button1_Click_1(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            string myff = "nihao";
            //string myff = printDocument1.PrinterSettings.PrinterName;
            string json = @"{'a':'一份','b':'水洗 x 3件，床品 x 3件，干洗 x 3件，洗鞋 x 5双','c':'抽丝,染色'}";
            JObject jo = (JObject)JsonConvert.DeserializeObject(json);
            string zone = jo["b"].ToString();
            MessageBox.Show(zone);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            //Class1.serverSocket.
            //MessageBox.Show("关闭了");
            //Environment.Exit(0);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            string ps = textBox2.Text;
            if (ps != "")
            {
                Form1.fontsize = Convert.ToInt32(ps);
            }
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string ppd = textBox1.Text;
            ppd = ppd.Trim();
            Form1.print_str = ppd;
        }
    }
}
